class Solution:
    def removeDuplicateLetters(self, s: str) -> str:
        mapper=[0] * 26
        stack=[]
        for c in s:
            mapper[ord(c)-ord('a')]+=1
        for c in s:
            if c not in stack:
                while stack and ord(stack[-1])>ord(c) and mapper[ord(stack[-1])-ord('a')]>0:
                    stack.pop()
                stack.append(c)
            mapper[ord(c)-ord('a')]-=1
        return "".join(stack)